<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分类" prop="categoryIds">
        <el-cascader style="width: 100%" v-model="queryParams.categoryIds" :options="categoryOptions" :props="props" collapse-tags clearable
          ><template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template></el-cascader
        >
      </el-form-item>
      <el-form-item label="信件编号" prop="letterNumber">
        <el-input v-model="queryParams.letterNumber" placeholder="请输入信件编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="来信人姓名" prop="letterUsername">
        <el-input v-model="queryParams.letterUsername" placeholder="请输入来信人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="来信人电话" prop="letterPhone">
        <el-input v-model="queryParams.letterPhone" placeholder="请输入来信人电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="查询码" prop="queryCode">
        <el-input v-model="queryParams.queryCode" placeholder="请输入查询码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="来信目的" prop="letterPurpose">
        <el-select v-model="queryParams.letterPurpose" placeholder="来信目的" clearable>
          <el-option v-for="dict in dict.type.biz_mailbox_purpose" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否公开" prop="isPublish">
        <el-select
          v-model="queryParams.isPublish"
          placeholder="是否公开"
          clearable
        >
          <el-option
            v-for="dict in dict.type.biz_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="是否超时" prop="isTimeout">
        <el-select v-model="queryParams.isTimeout" placeholder="是否超时" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否废弃" prop="isAbandon">
        <el-select
          v-model="queryParams.isAbandon"
          placeholder="是否废弃"
          clearable
        >
          <el-option
            v-for="dict in dict.type.biz_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="是否发布" prop="isPublic">
        <el-select
          v-model="queryParams.isPublic"
          placeholder="是否发布"
          clearable
        >
          <el-option
            v-for="dict in dict.type.biz_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="信件状态" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="信件状态" clearable>
          <el-option v-for="dict in dict.type.biz_mailbox_letter_finish" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="来信时间">
        <el-date-picker v-model="letterTime" :clearable="true" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @input="$forceUpdate()"></el-date-picker>
      </el-form-item>

      <el-form-item label="责任处室" prop="acceptDepts">
        <el-select v-model="queryParams.acceptDepts" placeholder="责任处室" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_letter_resp_dept" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="分类状态" prop="isCategory">
        <el-select v-model="queryParams.isCategory" placeholder="分类状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_whether_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['leader:mailbox:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['leader:mailbox:edit']"
          >分类</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['leader:mailbox:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['leader:mailbox:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['leader:mailbox:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="downloadTemplate('letter_mailbox_import')">下载导入模板</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mailboxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="一级分类" align="center" prop="firstCategoryName" />
      <el-table-column label="二级分类" align="center" prop="secondCategoryName" />
      <el-table-column label="信件编号" align="center" prop="letterNumber" width="150" />
      <el-table-column label="来信人姓名" align="center" prop="letterUsername" />
      <el-table-column label="来信人电话" align="center" prop="letterPhone" />
      <el-table-column label="查询码" align="center" prop="queryCode" />
      <el-table-column label="来信目的" align="center" prop="letterPurpose">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_mailbox_purpose" :value="scope.row.letterPurpose" />
        </template>
      </el-table-column>

      <el-table-column label="责任处室" align="center" prop="acceptDepts" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_resp_dept" :value="scope.row.acceptDepts" />
        </template>
      </el-table-column>
      <el-table-column label="信件标题" align="center" prop="letterTitle" :show-overflow-tooltip="true" />
      <!-- <el-table-column
        label="信件内容"
        align="center"
        prop="letterContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="回复内容"
        align="center"
        prop="replyContent"
        :show-overflow-tooltip="true"
      /> -->

      <el-table-column label="来信时间" align="center" prop="letterTime" width="150" />
      <el-table-column label="回复时间" align="center" prop="replyDate" width="150" />

      <el-table-column label="信件状态" align="center" prop="isFinish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_mailbox_letter_finish" :value="scope.row.isFinish" />
        </template>
      </el-table-column>
      <el-table-column label="是否公开" align="center" prop="isPublish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isPublish" />
        </template>
      </el-table-column>
      <el-table-column label="是否超时" align="center" prop="isTimeout">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isTimeout" />
        </template>
      </el-table-column>
      <el-table-column label="是否废弃" align="center" prop="isAbandon">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isAbandon" />
        </template>
      </el-table-column>
      <el-table-column label="是否发布" align="center" prop="isPublic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isPublic" />
        </template>
      </el-table-column>
      <el-table-column label="超期时间" align="center" prop="delayDate" width="150"> </el-table-column>
      <el-table-column label="导入时间" align="center" prop="createTime" width="150"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)" v-hasPermi="['leader:mailbox:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 2)" v-hasPermi="['leader:mailbox:edit']">分类</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['leader:mailbox:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改省住建厅官网厅长信箱对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="categoryIds">
              <el-cascader style="width: 100%" v-model="form.categoryIds" :options="categoryOptions" :props="{ expandTrigger: 'hover' }" clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信件编号" prop="letterNumber">
              <!-- <el-input
                v-model="form.letterNumber"
                placeholder="请输入信件编号"
              /> -->
              {{ form.letterNumber }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="查询码" prop="queryCode">
              <!-- <el-input
                v-model="form.queryCode"
                placeholder="请输入查询码"
              /> -->
              {{ form.queryCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来信人ip" prop="letterIp">
              <!-- <el-input
                v-model="form.queryCode"
                placeholder="请输入查询码"
              /> -->
              {{ form.letterIp }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="来信人姓名" prop="letterUsername"> <el-input v-model="form.letterUsername" placeholder="请输入来信人姓名" /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="来信人电话" prop="letterPhone"> <el-input v-model="form.letterPhone" placeholder="请输入来信人电话" /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="来信目的" prop="letterPurpose">
              <el-select v-model="form.letterPurpose" placeholder="来信目的" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_mailbox_purpose" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任处室" prop="acceptDepts">
              <el-select multiple v-model="form.acceptDepts" placeholder="责任处室" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_letter_resp_dept" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="信件标题" prop="letterTitle"> <el-input v-model="form.letterTitle" placeholder="请输入内容" /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="信件内容"> <editor v-model="form.letterContent" :min-height="150" /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="回复内容"> <editor v-model="form.replyContent" :min-height="150" /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="来信时间" prop="letterTime"> <el-date-picker style="width: 100%" clearable v-model="form.letterTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择来信时间"> </el-date-picker> </el-form-item
          ></el-col>

          <el-col :span="12">
            <el-form-item label="回复时间" prop="replyDate"> <el-date-picker style="width: 100%" clearable v-model="form.replyDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择回复时间"> </el-date-picker> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="信件状态" prop="isFinish">
              <el-select v-model="form.isFinish" placeholder="信件状态" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_mailbox_letter_finish" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="超期时间" prop="delayDate"> <el-date-picker style="width: 100%" clearable v-model="form.delayDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择超期时间"> </el-date-picker> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否发布" prop="isPublish">
              <el-radio-group v-model="form.isPublish">
                <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否超时" prop="isTimeout">
              <el-radio-group v-model="form.isTimeout">
                <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否公开" prop="isPublic">
              <el-radio-group v-model="form.isPublic">
                <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否废弃" prop="isAbandon">
              <el-radio-group v-model="form.isAbandon">
                <el-radio-button v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark"> <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" /> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="view == 2" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport + '&importType=' + upload.importType" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <!-- <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的数据 -->
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMailbox, getMailbox, delMailbox, addMailbox, updateMailbox } from "@/api/hades/leader/mailbox";
import { treeselect } from "@/api/hades/category/category";
import { getToken } from "@/utils/auth";

export default {
  name: "Mailbox",
  dicts: ["biz_yes_no", "biz_mailbox_purpose", "biz_mailbox_letter_finish", "biz_letter_resp_dept", "biz_whether_category"],
  data() {
    return {
      props: { multiple: true },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 省住建厅官网厅长信箱表格数据
      mailboxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/letter/import/importData",
        importType: "",
        categoryIds: [],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        letterUsername: null,
        letterPhone: null,
        queryCode: null,
        letterPurpose: null,
        letterTitle: null,
        status: null,
        isPublish: null,
        isTimeout: null,
        isAbandon: null,
        isPublic: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryIds: [{ required: true, message: "请选择分类", trigger: "change" }],
      },
      categoryOptions: [],
      view: 2,
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    /** 查询分类列表 */
    getCategoryList() {
      treeselect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    /** 查询省住建厅官网厅长信箱列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.letterTime, "LetterTime");
      listMailbox(this.queryParams).then((response) => {
        this.mailboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        categoryId: null,
        letterNumber: null,
        letterUsername: null,
        letterPhone: null,
        queryCode: null,
        letterPurpose: null,
        deptName: null,
        letterTitle: null,
        letterContent: null,
        replyContent: null,
        letterTime: null,
        delFlag: null,
        status: null,
        isPublish: null,
        isTimeout: null,
        isAbandon: null,
        isPublic: null,
        delayDate: null,
        replyDate: null,
        letterIp: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.letterTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加省住建厅官网厅长信箱";
    },
    /** 修改按钮操作 */
    handleUpdate(row, view) {
      this.view = view;
      this.reset();
      const id = row.id || this.ids;
      getMailbox(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改省住建厅官网厅长信箱";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMailbox(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMailbox(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除省住建厅官网厅长信箱编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMailbox(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "leader/mailbox/export",
        {
          ...this.queryParams,
        },
        `mailbox_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
      this.upload.importType = "MAILBOX";
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    downloadTemplate(command) {
      let a = document.createElement("a");
      a.href = `/static/` + command + `.xlsx`;
      a.download = command + ".xlsx";
      a.click();
    },
  },
};
</script>
